package org.dromara.bean.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.dromara.bean.domain.model.HisRegionModel;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.dromara.bean.domain.bo.HisMemberBo;
import org.dromara.bean.domain.vo.HisMemberVo;
import org.dromara.bean.domain.HisMember;
import org.dromara.bean.mapper.HisMemberMapper;
import org.dromara.bean.service.IHisMemberService;

import java.util.List;
import java.util.Collection;

/**
 * 患者Service业务层处理
 *
 * @author Lion Li
 * @date 2024-11-23
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class HisMemberServiceImpl implements IHisMemberService {

    private final HisMemberMapper baseMapper;

    /**
     * 查询患者
     *
     * @param id 主键
     * @return 患者
     */
    @Override
    public HisMemberVo queryById(Long id){
        return baseMapper.selectVoById(LoginHelper.getUserId());
    }

    /**
     * 分页查询患者列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 患者分页列表
     */
    @Override
    public TableDataInfo<HisMemberVo> queryPageList(HisMemberBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HisMember> lqw = buildQueryWrapper(bo);
        Page<HisMemberVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的患者列表
     *
     * @param bo 查询条件
     * @return 患者列表
     */
    @Override
    public List<HisMemberVo> queryList(HisMemberBo bo) {
        LambdaQueryWrapper<HisMember> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HisMember> buildQueryWrapper(HisMemberBo bo) {
        LambdaQueryWrapper<HisMember> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), HisMember::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getNickName()), HisMember::getNickName, bo.getNickName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhoto()), HisMember::getPhoto, bo.getPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getIdCard()), HisMember::getIdCard, bo.getIdCard());
        lqw.like(StringUtils.isNotBlank(bo.getMobile()), HisMember::getMobile, bo.getMobile());
        lqw.eq(StringUtils.isNotBlank(bo.getBirthday()), HisMember::getBirthday, bo.getBirthday());
        lqw.eq(bo.getGender() != null, HisMember::getGender, bo.getGender());
//        lqw.eq(StringUtils.isNotBlank(bo.getOpenId()), HisMember::getOpenId, bo.getOpenId());
        lqw.orderByDesc(HisMember::getCreateTime);
        return lqw;
    }

    /**
     * 新增患者
     *
     * @param bo 患者
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(HisMemberBo bo) {
        HisMember add = MapstructUtils.convert(bo, HisMember.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改患者
     *
     * @param bo 患者
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(HisMemberBo bo) {
        log.error("修改患者:{}",bo);
        HisMember update = MapstructUtils.convert(bo, HisMember.class);
        update.setId(LoginHelper.getUserId());
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(HisMember entity){
        entity.setId(LoginHelper.getUserId());
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除患者信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }


    @Override
    public HisMemberVo selectUserByOpenid(String openId) {
        return baseMapper.selectVoOne(Wrappers.lambdaQuery(HisMember.class).eq(HisMember::getOpenId, openId).eq(HisMember::getDelFlag, 0));
    }


    @Override
    public List<HisRegionModel> listByAll(String cityName) {
        return baseMapper.regionByAll(cityName);
    }

    @Override
    public List<HisRegionModel> listAll() {
        return baseMapper.regionAll();
    }

    @Override
    public HisRegionModel getByRegionCode(String code) {
        return baseMapper.getByRegionCode(code);
    }

    @Override
    public Boolean edit(HisMemberBo bo) {
        log.error("修改患者:{}",bo);
        HisMember update = MapstructUtils.convert(bo, HisMember.class);
        return baseMapper.updateById(update) > 0;
    }
}
